﻿using IOP.Extension.DependencyInjection;
using IOP.SgrA;
using IOP.SgrA.ECS;
using IOP.SgrA.Input;
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Text;

namespace VkSample103
{
    public class InputSystem : ComponentSystem
    {
        [Autowired]
        private IGraphicsManager GraphicsManager { get; set; }
        private Camera Camera;
        private bool flag = false;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="contextManager"></param>
        public InputSystem()
        {
        }

        public override void Initialization()
        {
            GraphicsManager.TryGetCamera("main", out Camera camera);
            Camera = camera;
        }

        public override void Update(TimeSpan timeSpan)
        {
            if (Input.GetMouseButtonState(MouseButton.ButtonLeft).Action == InputAction.Press)
            {
                var delta = Input.GetMousePositionDelta();
                if (delta.X != 0 || delta.Y != 0)
                {
                    Camera.EncircleFromMouse(Vector3.Zero, delta.X, delta.Y, (float)(0.2 * Math.PI / 180.0));
                    EventBus.Publish("CameraChanged", Camera, EventTrigger.SystemFrameAfter);
                }
            }
            else if(Input.GetMouseButtonState(MouseButton.ButtonLeft).Action == InputAction.Click)
            {
                flag = !flag;
            }
            ContextManager.Foreach<DiscardGroup>((group) =>
            {
                Context context = ContextManager.GetContext(group.Entity);
                IRenderGroup discard = context.GetContextRenderGroup();
                discard.PushDataToTexture(flag ? 1 : 0, 0, 1);
                context.PushToRenderGroup();
            });
        }

        public struct DiscardGroup
        {
            public Entity Entity;
            public DiscardComponent Discard;
        }
    }
}
